﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000340 RID: 832
	internal class WhereObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06001130 RID: 4400 RVA: 0x0004F224 File Offset: 0x0004D624
		public WhereObservable(IObservable<T> source, Func<T, bool> predicate) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.predicate = predicate;
		}

		// Token: 0x06001131 RID: 4401 RVA: 0x0004F240 File Offset: 0x0004D640
		public WhereObservable(IObservable<T> source, Func<T, int, bool> predicateWithIndex) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.predicateWithIndex = predicateWithIndex;
		}

		// Token: 0x06001132 RID: 4402 RVA: 0x0004F25C File Offset: 0x0004D65C
		public IObservable<T> CombinePredicate(Func<T, bool> combinePredicate)
		{
			if (this.predicate != null)
			{
				return new WhereObservable<T>(this.source, (T x) => this.predicate(x) && combinePredicate(x));
			}
			return new WhereObservable<T>(this, combinePredicate);
		}

		// Token: 0x06001133 RID: 4403 RVA: 0x0004F2AC File Offset: 0x0004D6AC
		public IObservable<TR> CombineSelector<TR>(Func<T, TR> selector)
		{
			if (this.predicate != null)
			{
				return new WhereSelectObservable<T, TR>(this.source, this.predicate, selector);
			}
			return new SelectObservable<T, TR>(this, selector);
		}

		// Token: 0x06001134 RID: 4404 RVA: 0x0004F2D3 File Offset: 0x0004D6D3
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			if (this.predicate != null)
			{
				return this.source.Subscribe(new WhereObservable<T>.Where(this, observer, cancel));
			}
			return this.source.Subscribe(new WhereObservable<T>.Where_(this, observer, cancel));
		}

		// Token: 0x04000A28 RID: 2600
		private readonly IObservable<T> source;

		// Token: 0x04000A29 RID: 2601
		private readonly Func<T, bool> predicate;

		// Token: 0x04000A2A RID: 2602
		private readonly Func<T, int, bool> predicateWithIndex;

		// Token: 0x02000341 RID: 833
		private class Where : OperatorObserverBase<T, T>
		{
			// Token: 0x06001135 RID: 4405 RVA: 0x0004F307 File Offset: 0x0004D707
			public Where(WhereObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001136 RID: 4406 RVA: 0x0004F318 File Offset: 0x0004D718
			public override void OnNext(T value)
			{
				bool flag = false;
				try
				{
					flag = this.parent.predicate(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				if (flag)
				{
					this.observer.OnNext(value);
				}
			}

			// Token: 0x06001137 RID: 4407 RVA: 0x0004F390 File Offset: 0x0004D790
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001138 RID: 4408 RVA: 0x0004F3C8 File Offset: 0x0004D7C8
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A2B RID: 2603
			private readonly WhereObservable<T> parent;
		}

		// Token: 0x02000342 RID: 834
		private class Where_ : OperatorObserverBase<T, T>
		{
			// Token: 0x06001139 RID: 4409 RVA: 0x0004F400 File Offset: 0x0004D800
			public Where_(WhereObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.index = 0;
			}

			// Token: 0x0600113A RID: 4410 RVA: 0x0004F418 File Offset: 0x0004D818
			public override void OnNext(T value)
			{
				bool flag = false;
				try
				{
					flag = this.parent.predicateWithIndex(value, this.index++);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				if (flag)
				{
					this.observer.OnNext(value);
				}
			}

			// Token: 0x0600113B RID: 4411 RVA: 0x0004F4A0 File Offset: 0x0004D8A0
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0600113C RID: 4412 RVA: 0x0004F4D8 File Offset: 0x0004D8D8
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A2C RID: 2604
			private readonly WhereObservable<T> parent;

			// Token: 0x04000A2D RID: 2605
			private int index;
		}
	}
}
